from django.db import models
from django.contrib.auth.hashers import check_password, make_password
#用户表
class User(models.Model):
    account = models.CharField(max_length=1024, verbose_name='账号', blank=True, null=True)
    password = models.CharField(max_length=1024, verbose_name='密码')
    phone = models.CharField(max_length=11, verbose_name='手机号', unique=True)
    email = models.CharField(max_length=1024, verbose_name='邮箱', blank=True, null=True)
    username = models.CharField(max_length=1024, verbose_name='用户名', blank=True, null=True)
    avatar = models.CharField(max_length=1024, verbose_name='头像', blank=True, null=True)
    is_wechat_bound = models.BooleanField(default=False, verbose_name='是否绑定微信')
    wechat_openid = models.CharField(max_length=1024, blank=True, null=True, verbose_name='微信OpenID')
    is_qq_bound = models.BooleanField(default=False, verbose_name='是否绑定QQ')
    qq_openid = models.CharField(max_length=1024, blank=True, null=True, verbose_name='QQ OpenID')
    def set_password(self, password):
        self.password = make_password(password)
    def check_password(self, password):
        return check_password(password, self.password)
    class Meta:
        db_table = 'user'
#分类
class Categories(models.Model):
    category = models.CharField(max_length=1024, verbose_name='分类名')
    class Meta:
        db_table = 'categories'
#文章表
class Articles(models.Model):
    title = models.CharField(max_length=1024, verbose_name='标题')
    author = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='作者')
    content = models.TextField(verbose_name='内容')
    intro = models.CharField(max_length=1024, verbose_name='引子')
    read = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='阅读量')
    like = models.IntegerField(default=0, verbose_name='点赞量')
    view = models.IntegerField(default=0, verbose_name='评论量')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    category = models.ForeignKey(Categories, on_delete=models.CASCADE, verbose_name='分类',db_index=True)
    tags = models.ManyToManyField('Tag',  verbose_name='标签名')
    class Meta:
        db_table = 'articles'

#标签表
class Tag(models.Model):
    title = models.CharField(max_length=1024, verbose_name='标签')
    class Meta:
        db_table = 'tag'
#评论表
class Comment(models.Model):
    content = models.TextField(verbose_name='评论内容')
    article = models.ForeignKey(Articles, on_delete=models.CASCADE, related_name='comments', verbose_name='关联文章')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='评论用户')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='replies',verbose_name ='父评论')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
    class Meta:
        db_table = 'comment'